/*
 * Radio.c
 *
 * Created: 2012-07-09 16:28:30
 *  Author: Sebastian_2
 */ 

#include "Menus.h"

void Menu_Radio_Enter() {
	gMenu = MENU_RADIO;
	point p = {147, 48};
	point p2 = {245, 122};
	gColour = SUBMENU_BACKGROUND;
	SSD1289_DrawRect(&p, &p2);
	drawFrame(&p, &p2, SEPARATOR);
	gColour = FONT_COLOUR;
	p = (point){152, 54};
	SSD1289_PrintString("Stacje", &p, &droidSans22ptFontInfo);
	p = (point){147, 84};
	drawXSeparator(&p, 245, SEPARATOR);
	p = (point){152, 90};
	SSD1289_PrintString("Wylacz", &p, &droidSans22ptFontInfo);
}

void Menu_Radio_BackToMain() {
	gMenu = MENU_MAIN;
	point p = {147, 48};
	point p2 = {246, 123};
	gColour = BACKGROUND;
	SSD1289_DrawRect(&p, &p2);
	p = (point){169, 56};
	ShowRGB565("/ico/calendar.565", &p);
}

void Menu_Radio_TurnOff() {
	// do some I2C magic	
}

void Menu_Radio_Stations_Enter() {
	mRStationsCurrentPage = 0;
	Menu_Radio_Stations_Refresh();
}

void Menu_Radio_Stations_Refresh() {
	gMenu = MENU_RADIO_STATIONS;
	
	FIL file;
	FRESULT res;
	char tmp[35];
	uint8_t pos;
	point p = {10, 28};
	
	pos = mRStationsCurrentPage*ITEMS_ON_PAGE + mRStationsSelectedRow;
	res = f_open(&file, "/radio", FA_READ | FA_OPEN_EXISTING);
	CheckRes();
	f_gets(tmp, sizeof(tmp), &file);
	sscanf(tmp, "%u", (uint16_t*)&mRStationsMaxItems);
	
	if(pos >= mRStationsMaxItems-1)
		return;
	
	for(uint8_t i = 0; i < mRStationsCurrentPage*ITEMS_ON_PAGE; i++) {
		f_gets(tmp, sizeof(tmp), &file);
	}
	
	SSD1289_ClearOmitBar();
	for(uint8_t i = 0; i < ITEMS_ON_PAGE; i++) {
		if(f_gets(tmp, sizeof(tmp), &file) == tmp) {
			char separator = 0x03;
			char *ptr;
			
			ptr = strtok(tmp, &separator);
			SSD1289_PrintString(ptr, &p, &droidSans22ptFontInfo);
			p = IncY(&p, 30); p.x = 0;
			drawXSeparator(&p, SSD1289_MaxX, SEPARATOR2);
			p = IncY(&p, 6); p.x = 10;
		}
	}
	f_close(&file);
}